class code4 {
    List<Integer> path;
    List<List<Integer>> ret;
    boolean[] check;
    int n;
    public List<List<Integer>> permute(int[] nums) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        n = nums.length;
        check = new boolean[n];
        dfs(nums);
        return ret;
    }

    public void dfs(int[] nums) {
        if(path.size() == n) {
            ret.add(new ArrayList<>(path));
        }

        for(int i=0;i<n;i++) {
            if(!check[i]) {
                path.add(nums[i]);
                check[i] = true;
                dfs(nums);
                //回溯
                check[i] = false;
                path.remove(path.size()-1);
            }
        }
    }
}